﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace MathAssist
{
    public partial class frmMathAssist : Form
    {
        InterpretCommand interpret;
        Stopwatch sw;

        public frmMathAssist() {
            InitializeComponent();
        }

        private void frmMathAssist_Load(object sender, EventArgs e) {
            ScanPlug scan = new ScanPlug(tsmiPlug);
            try {
                interpret = new InterpretCommand(scan.Refurbish(Application.StartupPath + "\\plug"));
                foreach (MathAssistLibrary.ICommand i in interpret.Commands) {
                    lstCommand.Items.Add(i.Name);
                }
            } catch (DirectoryNotFoundException value) {
                MessageBox.Show("没有找到plug目录");
            }
        }

        private void rtfCommand_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Enter) {
                e.Handled = true;
                sw = Stopwatch.StartNew();
                rtfResult.Text += rtfCommand.Text + "\n" + interpret.Interpret(rtfCommand.Text) + "\n======\n";
                sw.Stop();
                stLabelTime.Text = string.Format("命令执行用时:{0}秒", sw.Elapsed.TotalSeconds.ToString("F3"));
            }
        }

        private void lstCommand_SelectedIndexChanged(object sender, EventArgs e) {
            txtDescribe.Text = interpret.Commands[lstCommand.SelectedIndex].Describe;
        }
        
        private void toolStripButton_Click(object sender, EventArgs e) {
            ToolStripButton btn = sender as ToolStripButton;
            char c = Convert.ToChar(Convert.ToInt32(btn.Tag));
            string text = rtfCommand.Text;
            int start = rtfCommand.SelectionStart;
            text = text.Insert(rtfCommand.SelectionStart, c.ToString());
            rtfCommand.Text = text;
            rtfCommand.SelectionStart = start + 1;
        }

        private void tsmiClear_Click(object sender, EventArgs e) {
            rtfResult.Text = "";
        }

        private void rtfCommand_TextChanged(object sender, EventArgs e) {

        }

        private void tsmiCommandShow_Click(object sender, EventArgs e) {
            splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
            tsmiCommandShow.Checked = !splitContainer1.Panel1Collapsed;
        }

        private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void tsmiAbout_Click(object sender, EventArgs e) {
            AboutBox1 frm = new AboutBox1();
            frm.ShowDialog();
        }
    } // end class frmMathAssist
}
